package com.qiaotongtianxia.loans.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * Created by Administrator on 2017/5/25.
 * lisen
 */

public class SPUtil {


    //可清理
    public static final String ACCOUNTS = "accounts";//登录过的账号---------->文件

    //可清理
    public static final String ACCOUNT_INFO = "account_info";//当前登录的账号 密码信息---------->文件
    public static final String ACCOUNT_INFO_KEY = "account_info_key";//登录者的账号名 密码信息

    //可清理
    public static final String APPTOKEN = "apptoken";//保存token---------->文件
    public static final String APPTOKEN_KEY = "apptoken_key";//保存的token  key

    public static final String MEMBER = "member";//登录者的用户信息
    public static final String MEMBER_KEY = "member_key";



    private static SharedPreferences sp;

    /**
     * @param context
     * @param spName  保存的文件名
     * @param key
     * @param object  具体数据
     */
    public static void save(Context context, String spName, String key, Object object) {

        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else if(object == null){
            editor.putString(key, "");
        }else{
            editor.putString(key, object.toString());
        }

        editor.commit();
    }

    public static String getString(Context context, String spName, String key, String defValue) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    public static int getInt(Context context, String spName, String key, int defValue) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static long getLong(Context context, String spName, String key, long defValue) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    public static float getFloat(Context context, String spName, String key, float defValue) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getFloat(key, defValue);
    }

    public static boolean getBoolean(Context context, String spName, String key, boolean defValue) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }

    /**
     * 移除某个key值已经对应的值
     */
    public static void remove(Context context, String spName, String key) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }

    /**
     * 清除所有数据
     */
    public static void clear(Context context, String spName) {

        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    public static Map<String, String> getAll(Context context, String spName) {

        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return (Map<String, String>) sp.getAll();
    }
}
